#define _CRT_SECURE_NO_WARNINGS 1


/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */
struct ListNode* removeElements(struct ListNode* head, int val) {
    struct ListNode* newnode, * newtail;
    newnode = newtail = NULL;
    struct ListNode* cur = head;
    while (cur)
    {
        if (cur->val != val)
        {
            if (newnode == NULL)
            {
                newnode = newtail = cur;
            }
            else
            {
                newtail->next = cur;
                newtail = newtail->next;
            }
        }
        cur = cur->next;
    }
    if (newtail)
        newtail->next = NULL;
    return newnode;
}